package com.example.meeting.controller;

import cn.hutool.json.JSONObject;
import com.example.meeting.config.MyHandler;
import com.example.meeting.model.domain.Meeting;
import com.example.meeting.model.domain.MeetingCreateRequest;
import com.example.meeting.model.responseData.CommonResult;
import com.example.meeting.service.IMeetingService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class MeetingController {
    @Autowired
    private IMeetingService meetingService;

    /**
     * 查询对应id的会议信息
     * @param id
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/meeting/{id}")
    public CommonResult selectMeetingById(@PathVariable Long id) {
        Meeting meeting = meetingService.selectMeetingById(id);
        if (meeting == null) {
            return CommonResult.fail("查询失败，没有该会议！");
        }
        return CommonResult.success(meeting);
    }

    /**
     * 分页动态查询所有会议信息
     * @param page
     * @param pageSize
     * @param meeting    查询条件的会议对象，哪一个有值查询对应的会议列表，多个值则查询同时满足条件的会议
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/meetingList")
    public CommonResult selectMeetingList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                          @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "user_id") Integer user_id, // 明确指定参数名
                                          @RequestBody Meeting meeting) {
        PageInfo<Meeting> meetingList = meetingService.selectMeetingList(page,pageSize,user_id,meeting);
        if (meetingList == null) {
            return CommonResult.fail("查询失败，没有任何会议信息");
        }
        return CommonResult.success(meetingList);
    }

    /**
     * 新增会议信息
     * @param request
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/meeting")
    public CommonResult addMeeting(@Validated @RequestBody MeetingCreateRequest request) {
        Meeting meeting = new Meeting();
        BeanUtils.copyProperties(request, meeting); // 复制公共字段
        meeting.setStatus(0);
        Integer res = meetingService.addMeeting(meeting, request.getUser_id());
        if (res == 0) {
            return CommonResult.fail("新增会议失败！");
        }
        return CommonResult.success(meeting.getId());
    }

    /**
     * 更新会议信息
     * @param meeting
     * @return
     */
    @RequiresAuthentication
    @PutMapping("/meeting")
    public CommonResult updateMeeting(@RequestBody Meeting meeting) {
        if (meeting.getId() == null) {
            return CommonResult.fail("会议id不能为空");
        }
        Integer res = meetingService.updateMeeting(meeting);
        if (res == 0) {
            return CommonResult.fail("会议信息更新失败");
        }
        // 通知前端重新拉取议题信息
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("type","repullMeeting");
        MyHandler.sendMessage(meeting.getId(), jsonObject.toString());
        return CommonResult.success(res);
    }

    /**
     * 删除对应id的会议信息
     * @param id
     * @return
     */
    @RequiresAuthentication
    @DeleteMapping("/meeting/{id}")
    public CommonResult deleteMeeting(@PathVariable Long id) {
        Integer res = meetingService.deleteMeeting(id);
        if (res == 0) {
            return CommonResult.fail("删除会议失败");
        }
        return CommonResult.success(res);
    }

    /**
     * 开始会议
     */
    @RequiresAuthentication
    @PutMapping("/meeting/begin")
    public CommonResult beginMeeting(@RequestBody Meeting meeting){
        if (meeting==null || meeting.getId()==null){
            return  CommonResult.fail("缺少请求参数");
        }
        //设置会议状态为进行中
        Meeting data = new Meeting();
        data.setId(meeting.getId());
        data.setStatus(1);
        Integer res = meetingService.updateMeeting(data);
        if (res==0){
            return CommonResult.fail("开始会议失败，请重试！");
        }
        // 通知前端重新拉取议题信息
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("type","repullMeeting");
        MyHandler.sendMessage(meeting.getId(), jsonObject.toString());
        return CommonResult.success();
    }

    /**
     * 结束会议
     */
    @RequiresAuthentication
    @PutMapping("/meeting/end")
    public CommonResult endMeeting(@RequestBody Meeting meeting){
        if (meeting==null || meeting.getId()==null){
            return  CommonResult.fail("缺少请求参数");
        }
        //设置会议状态为已结束
        Meeting data = new Meeting();
        data.setId(meeting.getId());
        data.setStatus(2);
        Integer res = meetingService.updateMeeting(data);
        if (res==0){
            return CommonResult.fail("结束会议失败，请重试！");
        }
        // 通知前端重新拉取议题信息
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("type","repullMeeting");
        MyHandler.sendMessage(meeting.getId(), jsonObject.toString());
        return CommonResult.success();
    }
}
